package mlz.a_properties;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/*
    Properties属性集
 */
public class PropertiesDemo01 {
    @Test
    public void test01() throws IOException {
        //1.创建属性集对象
        Properties pro = new Properties();

        //2.加载属性集文件
        pro.load(new FileReader("prop.properties"));
        System.out.println(pro);

        //3.根据键获取对应值
        String name = pro.getProperty("张三");
        System.out.println(name);
        System.out.println(pro.getProperty("张无忌"));
        System.out.println(pro.getProperty("root"));

        System.out.println("---------------------");

        //遍历全部的键和值
        //获取键的集合
        Set<String> set = pro.stringPropertyNames();
        for(String key : set){
            //再根据key获取对应的value
            String value = pro.getProperty(key);
            System.out.println("key:" + key +":" + "value:" + value);
        }
    }


    @Test
    public void test02() throws IOException {
        Properties pro = new Properties();
        //2.设置文件中的值
        pro.setProperty("张三","18");
        pro.setProperty("李四","20");
        pro.setProperty("王五","30");
        //3. 把pro对象中的值添加到配置文件中
        pro.store(new FileWriter("prop2.properties"),"名字 和 年龄");
    }

}















